package com.weng.platform.apiManager.doc.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.apiManager.doc.entity.Catalog;
import com.weng.platform.apiManager.doc.model.vo.CatalogTreeNode;
import com.weng.platform.apiManager.doc.model.vo.CatalogVo;
import com.weng.platform.apiManager.doc.service.ICatalogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * api分类 API实现
 *
 * @author wengzhonghui
 * @date 2020-09-26 23:44:06
 *
 */  
@Slf4j
@RestController
@RequestMapping("/catalog")
@Api(value = "api分类api", tags = {"api分类操作接口"})
public class CatalogApi extends BaseController {
    @Autowired
    private ICatalogService catalogService;

	@ResponseBody
    @RequestMapping( value = "/get-tree-grid-data" ,method=RequestMethod.GET)
    @ApiOperation(value = "获取树形表格数据", notes = "获取树形表格数据")
    public ResponseData getTreeGridData(String name,String parentId,String isExpand, String projectId) {

        List<CatalogTreeNode> treeData = this.catalogService.getTreeGridData(name, parentId, isExpand, projectId);
        return ResponseData.success(treeData);
    }
	
    /**
     * 功能描述:单个获取api分类
     */
    @ApiOperation(value = "获取api分类", notes = "")
    @GetMapping(value = "/get")
    public ResponseData get(String id){
        Catalog catalog = catalogService.getById(id);
        return success(catalog);
    }


    /**
     * 条件 分页查询api分类
     */
    @ApiOperation(value = "分页获取api分类", notes = "")
    @GetMapping(value = "/list")
    public ResponseData list(Integer pageNo,Integer pageSize){
		IPage<Catalog> iPage = catalogService.list(pageNo, pageSize);
        return success(iPage);
    }


    /**
	 * 新增api分类
	 */
	@ApiOperation(value = "新增api分类", notes = "")
	@PostMapping(value = "/add")
	public ResponseData add(@Valid @RequestBody CatalogVo catalogReq){
		Catalog catalog = Convert.convert(Catalog.class, catalogReq);
		catalogService.add(catalog);
		return success();
	}

	/**
	 * 更新api分类
	 */
	@ApiOperation(value = "更新api分类", notes = "")
	@PostMapping(value = "/update")
	public ResponseData update(@Valid @RequestBody CatalogVo catalogReq){
		Catalog catalog = Convert.convert(Catalog.class, catalogReq);
		return  catalogService.update(catalog);
	}


	/**
	 * 删除api分类
	 */
	@ApiOperation(value = "ids 删除api分类", notes = "可以删除多个，中间以,号分隔")
	@PostMapping(value = "/delete")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,请选择需要删除的记录！");
		}
		return catalogService.removeByIds(deleteReq.getIds());
	}
}
